package com.taotao.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import Utils.ObjectResult;

import com.github.pagehelper.PageInfo;
import com.taotao.pojo.ItemParam;
import com.taotao.service.ItemParamService;

@RequestMapping(value="item/param")
@Controller
public class ItemParamController {

	@Autowired
	private ItemParamService itemParamService;
	
	/**
	 * 根据商品类目id查找商品规格参数模板
	 * @param itemCatId
	 * @return
	 */
	@RequestMapping(value="{itemCatId}",method=RequestMethod.GET)
	public ResponseEntity<ItemParam> queryOne(@PathVariable("itemCatId")Long itemCatId){
		try {
			ItemParam t = new ItemParam();
			t.setItemCatId(itemCatId);
			ItemParam itemParam = itemParamService.queryOne(t);
			if(itemParam != null)
			    return ResponseEntity.status(HttpStatus.OK).body(itemParam);
			return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
	/**
	 * 新增规格参数模板
	 * @param itemCatId
	 * @param paramData
	 * @return
	 */
	@RequestMapping(value="{itemCatId}",method=RequestMethod.POST)
	public ResponseEntity<Void> saveOne(@PathVariable("itemCatId")Long itemCatId,
			                                 @RequestParam("paramData")String paramData){
		try {
			ItemParam t = new ItemParam();
			t.setItemCatId(itemCatId);
			t.setParamData(paramData);
			itemParamService.save(t);
		    return ResponseEntity.status(HttpStatus.CREATED).body(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
	@RequestMapping(method=RequestMethod.GET)
	public ResponseEntity<ObjectResult> queryAll(@RequestParam(value="page",defaultValue="1")Integer page,
			                                 @RequestParam(value="paramData",defaultValue="10")Integer rows){
		try {
			 ItemParam itemParam = new ItemParam();
			 PageInfo<ItemParam> pageInfo = itemParamService.queryPageInfoBywhere(itemParam,page,rows);
			 ObjectResult result = new ObjectResult();
			 result.setRows(pageInfo.getList());
			 result.setTotaLong(pageInfo.getTotal());
			 return ResponseEntity.status(HttpStatus.OK).body(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
}

